package com.yiqi.news.mvp.model

import android.app.Application
import com.google.gson.Gson
import com.jess.arms.integration.IRepositoryManager
import com.jess.arms.mvp.BaseModel

import com.jess.arms.di.scope.ActivityScope
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.entity.User
import javax.inject.Inject

import com.yiqi.news.mvp.contract.UserinfoContract
import com.yiqi.news.mvp.model.api.service.UserService
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/20/2019 16:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class UserinfoModel
@Inject
constructor(repositoryManager: IRepositoryManager) : BaseModel(repositoryManager), UserinfoContract.Model {
    override fun uploadAvatar(file: String): Observable<BaseResponse<User>> {
        val file = File(file)
        val requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, requestFile)
        return mRepositoryManager.obtainRetrofitService(UserService::class.java).uploadFile1(filePart)
    }

    override fun updateUser(map: HashMap<String, Any?>): Observable<BaseResponse<User>> {
        return mRepositoryManager.obtainRetrofitService(UserService::class.java).updateUser(map)
    }

    @Inject
    lateinit var mGson: Gson;
    @Inject
    lateinit var mApplication: Application;

    override fun getUser(): Observable<BaseResponse<User>> {
        return mRepositoryManager.obtainRetrofitService(UserService::class.java).userInfo()
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}
